'use strict'

const _ = require('lodash')
const moment = require('moment')
const Env = use('Env')
const { AliYunService } = require('../../Services')
const { News,FaExhibitor,FaProduct,UserProduct } = require("../../Models");
class ProductController {
  async index({ request, response }) {
    let { page = 1, pageSize = 10, searchWord, firstCate, exhibitorID = 0 } = request.all()

    let query = FaProduct.query()
    let list = null
    if (exhibitorID > 0) {
      list = await query.where('shop_id', exhibitorID).paginate(page, pageSize)
    } else {


      if (firstCate) {
        firstCate = JSON.parse(firstCate)
        console.log('firstCate', firstCate)
        for (let i = 0; i < firstCate.length; i++) {
          console.log('firstCateItem', firstCate[i])
          query.where('first_c_name', firstCate[i])
        }
      }
      if (searchWord=='0') searchWord = ''
      if (searchWord && searchWord.trim().length > 0) {
        console.log(searchWord)
        query.where('name', 'like', `%${searchWord}%`).orWhere(function () {
          this.where('product_material', 'like', `%${searchWord}%`)
            .orWhere('product_color', 'like', `%${searchWord}%`)
            .orWhere('company_name', 'like', `%${searchWord}%`)
        })
      }
      list = await query.with("exhibitor").paginate(page, pageSize)
    }
    response.json(list)
  }

  async show({ request, response, params }) {
    console.log('params: ', params);
    let product = await FaProduct.query().where('id',params.id).with("exhibitor").first()
    let c = await UserProduct.query().where('productID',params.id).getCount()
    product.likeCount = c
    response.json(product)
  }
}
module.exports = ProductController